<%@ LANGUAGE="JavaScript" codepage=65001%><?xml version="1.0" encoding="UTF-8"?>
<!-- #include file="AAC.asp" -->
<!-- #include file="Config.asp" -->
<%
//==================================
//  Weather for google earth v1.0
//  更新时间: 2006-06-26
//  embont@gmail.com
//  www.dnxh.cn
//==================================
Server.ScriptTimeOut = 180;
Response.CharSet = 65001 ;
Response.ContentType = "text/xml";

var alt  = Request.QueryString("CAMERA");
var BBOX = Request.QueryString("BBOX");
var _ary = String(BBOX).split(",");
var west    = _ary[0];
var south   = _ary[1];
var east    = _ary[2];
var north   = _ary[3];

var UpdataState = true;
var nowdate = new Date();
var nowyear = nowdate.getYear();
var nowday  = nowdate.getDate();
var nowhour = nowdate.getHours();

var vSQL;
	if (alt>7000*1000){
	//取最高级别的点
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+") AND scale<1";
	}else
	if (alt<7000*1000 && alt>4000*1000){
	//取最高级别的点
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+") AND scale<2";
	}else 
	//取省级以上点
	if (alt<=4000*1000 && alt>1000*1000){
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+") AND scale<3";
	}else{
	//取全部点
	vSQL = "gew WHERE (lon between "+west+" AND "+east+") AND (lat between "+south+" AND "+north+")";
	}
var oAAC = new AAC(mdbfile,vSQL);
	oAAC.open(3,1);
var oRS = oAAC.oRecordSet;

%><kml xmlns="http://earth.google.com/kml/2.2"><%

if (oRS.RecordCount == 0){
	    
	    //屏幕内未含有点，获取最近点

	    
		//清除前一个当前屏幕的记录集
	    oRS = "";
	    oAAC.close();
	    oAAC = "";
	    
		//新建一个记录集，用来筛选最近点。		
		var vAAC = new AAC(mdbfile,"gew");
		vAAC.open(3,1);
		var vRS = vAAC.oRecordSet;
		
        // 取得窗口中心点坐标位置
	    var Latitude  = (Number(south)+Number(north))/2;
	    var Longitude = (Number(west) +Number(east) )/2;
	    
		//最近点计算c*c=a*a+b*b
		var n = 0;
		var c = new Array();
		
		for(vRS.MoveLast();!vRS.BOF;vRS.MovePrevious()){
		n++;
		var vlon = vRS("lon");
		var vlat = vRS("lat");
		//计算最近距离
		var a   = Latitude - vlat;
		    a   = Math.abs(a)
		var b   = Longitude- vlon;
		    b   = Math.abs(b)		
		var c2  = a*a+b*b ;
		c.push(Math.sqrt(c2) + "|" + vRS("code"));
		}
		c.sort();
		var code = c[0].split("|")[1];
		
		//更新数据目标代码code
		updata(code);
	    	if(!UpdataState){
	    	   //更新失败请稍后手动更新
	    	   UpdataState = true;
     		}else{
	    	  //更新完毕
	    	}
	    vRS = "";
	    vAAC.close();
	    vAAC = "";
}else{
        for(oRS.MoveLast();!oRS.BOF;oRS.MovePrevious()){
           var code = oRS("code");
           updata(code);
           if(!UpdataState){
             //更新失败请稍后手动更新
             UpdataState = true;
           }
        }
        //更新完毕
	    oRS = "";
	    oAAC.close();
	    oAAC = "";
}
//<Placemark>
//<name>重要通知</name>
//<description>由于用户量极大，给服务器造成的压力是我没有想到的，以至于服务器提供商停止了我的脚本运行权限。所以现在停止缓存的自动更新，十分抱歉！有什么建议请到我的Blog留言http://www.dnxh.cn/blog</description>
//</Placemark>
%>
</kml><%
//获得并写入缓存
function upkml(code){
    try{
        var oXHy = Server.CreateObject("Msxml2.ServerXMLHTTP");
        var url  = "http://xoap.weather.com/weather/local/" + code + "?cc=*&unit=m&prod=xoap&par=" + Vpar + "&key=" + Vkey + "&dayf=10";
		oXHy.setTimeouts(1000,2000,1000,5000);
		oXHy.open("GET",url,false);
        oXHy.send();
        var xml = Server.CreateObject("MSXML2.DOMDocument");
        xml.loadXML(oXHy.responseText);
		xml.save(Server.MapPath("XML/"+code+".xml"))
		oXHy='';
		xml='';
		}catch(err){UpdataState=false;}
}
//判断是否最新  
function updata(code){
    var fso = Server.CreateObject("Scripting.FileSystemObject");
     if(!fso.FileExists(Server.MapPath("XML/"+code+".xml"))){
          upkml(code);
     }else{
          var file  = fso.GetFile(Server.MapPath("XML/"+code+".xml"));
          var date  = file.DateLastModified;
          var date = new String(date).split(" ");
          var week = date[0];
          var month = date[1];
          var day = date[2];
          var hour = new Number(new String(date[3]).split(":")[0]);
              hour = hour+updatatime;
          var year = date[5]; 
             if(hour<=nowhour || day!=nowday){ //如果不是当天的就更新
               upkml(code);
	         }
          }
}

%>